<?php

use PhpAmqpLib\Connection\AMQPStreamConnection;
require_once __DIR__ . '/vendor/autoload.php';

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest', '/');
$channel = $connection->channel();

$channel->exchange_declare('ex01', 'topic', false, false, false);

[$queuename, , ] = $channel->queue_declare('', false, false, false, false);

$routeKey = $argv[1];

$channel->queue_bind($queuename, 'ex01', $routeKey);

$callback = function ($msg) {
    echo $msg->delivery_info['routing_key'] . ':' . $msg->body . "\n";
};

$channel->basic_consume($queuename, '', false, true, false, false, $callback);

while (count($channel->callbacks)) {
    $channel->wait();
}
